package com.arctan.mhxy_utils_android.ui.activitys;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.arctan.mhxy_utils_android.R;
import com.arctan.mhxy_utils_android.ui.adapters.MainViewPagerAdapter;
import com.arctan.mhxy_utils_android.ui.customviews.NoScrollViewPager;
import com.arctan.mhxy_utils_android.ui.fragments.IncomeFragment;
import com.arctan.mhxy_utils_android.ui.fragments.IntroductionFragment;
import com.arctan.mhxy_utils_android.ui.fragments.StatisticsAnalysisFragment;
import com.arctan.mhxy_utils_android.utils.ButtonClickUtil;
import com.elvishew.xlog.XLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @BindView(R.id.ll_tab)
    LinearLayout llTab;
    @BindView(R.id.rl_home)
    RelativeLayout rlHome;
    @BindView(R.id.tv_tab_home)
    TextView tvTabHome;
    @BindView(R.id.rl_tongji)
    RelativeLayout rlTongji;
    @BindView(R.id.tv_tab_tongji)
    TextView tvTabTongji;
    @BindView(R.id.rl_gonglue)
    RelativeLayout rlGonglue;
    @BindView(R.id.tv_tab_gonglue)
    TextView tvTabGonglue;
    @BindView(R.id.viewpager)
    NoScrollViewPager viewpager;
    private MainViewPagerAdapter mainAdapter;
    private long exitTime = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        XLog.tag(TAG).e("onCreate");
        ButterKnife.bind(this);
        initView();
    }
    private  void initView(){
        List<Fragment> list = new ArrayList<>();
        list.add(new IncomeFragment());
        list.add(new StatisticsAnalysisFragment());
        list.add(new IntroductionFragment());
        mainAdapter = new MainViewPagerAdapter(getSupportFragmentManager(), list);
        viewpager.setOffscreenPageLimit(3);
        viewpager.setAdapter(mainAdapter);
        changeTab(true, false,false);
    }
    private void changeTab(boolean first_selected,
                           boolean second_selected,boolean third_selected) {
        tvTabHome.setSelected(first_selected);
        tvTabTongji.setSelected(second_selected);
        tvTabGonglue.setSelected(third_selected);
    }
    @OnClick({R.id.rl_home, R.id.rl_tongji, R.id.rl_gonglue})
    public  void onBtnClick(View view){
        if (ButtonClickUtil.isFastDoubleClick()){
            return;
        }
        switch (view.getId()){
            case R.id.rl_home:
                viewpager.setCurrentItem(0, false);
                changeTab(true, false,false);
                break;
            case R.id.rl_tongji:
                viewpager.setCurrentItem(1, false);
                changeTab(false, true,false);
                break;
            case R.id.rl_gonglue:
                viewpager.setCurrentItem(2, false);
                changeTab(false, false,true);
                break;
        }
    }


    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        XLog.tag(TAG).e("onNewIntent");
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        XLog.tag(TAG).e("onSaveInstanceState");
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            XLog.tag(TAG).e("onConfigurationChanged: 横屏");
        } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
            XLog.tag(TAG).e("onConfigurationChanged: 竖屏");
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                this.finish();
                System.exit(0);
                android.os.Process.killProcess(android.os.Process.myPid());
            }
            return true;
        }
        if (!isFinishing()) {
            return super.onKeyDown(keyCode, event);
        } else {
            return true;
        }
    }

}